package com.fast.alden.admin.web.controller.system;

import com.fast.alden.admin.annotation.OperationLog;
import com.fast.alden.admin.service.SysNoticeService;
import com.fast.alden.data.model.SysNotice;
import com.fast.alden.data.model.SysOperationType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "SysNoticeController", description = "系统通知公告管理")
@RestController
@RequestMapping("/notice")
public class SysNoticeController {
    @Resource
    private SysNoticeService noticeService;

    @Operation(summary = "分页查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/page")
    public Page<SysNotice> page(@RequestBody @Nullable SysNotice searchVo,
                                @RequestParam(name = "no", defaultValue = "0") int no,
                                @RequestParam(name = "size", defaultValue = "10") int size
    ) {
        return noticeService.search(searchVo, PageRequest.of(no, size, Sort.by(Sort.Direction.DESC, "createdTime")));
    }

    @Operation(summary = "根据ID查询", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @GetMapping("/retrieve/{id}")
    public SysNotice retrieve(@PathVariable("id") Long id) {
        return noticeService.retrieve(id);
    }

    @OperationLog(title = "通知公告管理", type = SysOperationType.CREATE)
    @Operation(summary = "创建通知公告", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/create")
    public Long create(@RequestBody SysNotice role) {
        return noticeService.create(role).getId();
    }

    @OperationLog(title = "通知公告管理", type = SysOperationType.UPDATE)
    @Operation(summary = "修改通知公告", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PutMapping("/update")
    public void update(@RequestBody SysNotice role) {
        noticeService.update(role);
    }

    @OperationLog(title = "通知公告管理", type = SysOperationType.DELETE)
    @Operation(summary = "删除通知公告", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @DeleteMapping("/remove")
    public void remove(@RequestParam("ids") List<Long> ids) {
        noticeService.remove(ids);
    }

    /**
     * 分配资源
     */
    @OperationLog(title = "通知公告管理", type = SysOperationType.MESSAGE)
    @Operation(summary = "发布通知公告", security = @SecurityRequirement(name = "openApiSecurityScheme"))
    @PostMapping("/publish/{noticeId}")
    public void publish(@PathVariable(name = "noticeId") @NotNull Long noticeId) {
        noticeService.publish(noticeId);
    }
}
